<?php
require_once (ROOT."comm/config.php");
/*
 * @brief url封装类，将常用的url请求操作封装在一起
 */
class URL {
	private $error;
	private $cookie;
	public function __construct() {
	}
	public function init($project_id, $project_secret, $redirectUrl) {
		// -------请求参数列表
		$keysArr = array (
				"projectId" => $project_id,
				"securtCode" => $project_secret,
				"redirectUrl" => $redirectUrl 
		);
		
		$response = $this->post ( JAVA_POST_URL, $keysArr );
		// echo $response;
		// --------检测错误是否发生
		$itsm = json_decode ( $response );
		
		// print_r($itsm);
		if ($itsm->errCode != 0) {
			// $this->error->showError($itsm->error, $itsm->error_description);
			return ( int ) $itsm->errCode;
		}
		return 0;
	}
	/**
	 * combineURL
	 * 拼接url
	 * 
	 * @param string $baseURL
	 *        	基于的url
	 * @param array $keysArr
	 *        	参数列表数组
	 * @return string 返回拼接的url
	 */
	public function combineURL($baseURL, $keysArr) {
		$combined = $baseURL . "?";
		$valueArr = array ();
		
		foreach ( $keysArr as $key => $val ) {
			$valueArr [] = "$key=$val";
		}
		
		$keyStr = implode ( "&", $valueArr );
		$combined .= ($keyStr);
		
		return $combined;
	}
	
	/**
	 * get_contents
	 * 服务器通过get请求获得内容
	 * 
	 * @param string $url
	 *        	请求的url,拼接后的
	 * @return string 请求返回的内容
	 */
	public function get_contents($url) {
		if (ini_get ( "allow_url_fopen" ) == "1") {
			$response = file_get_contents ( $url );
		} else {
			$httpobj = new Http;
			$response = $httpobj->request($url);
		}
		
		// -------请求为空
		// if(empty($response)){
		// $this->error->showError("50001");
		// }
		
		return $response;
	}
	
	/**
	 * get
	 * get方式请求资源
	 * 
	 * @param string $url
	 *        	基于的baseUrl
	 * @param array $keysArr
	 *        	参数列表数组
	 * @return string 返回的资源内容
	 */
	public function get($url, $keysArr) {
		$combined = $this->combineURL ( $url, $keysArr );
		return $this->get_contents ( $combined );
	}
	
	/**
	 * post
	 * post方式请求资源
	 * 
	 * @param string $url
	 *        	基于的baseUrl
	 * @param array $keysArr
	 *        	请求的参数列表
	 * @param int $flag
	 *        	标志位
	 * @return string 返回的资源内容
	 */
	public function post($url, $keysArr, $flag = 0) {
		$httpobj = new Http;
		if (!$keysArr)
		{
			$keysArr = true;
		}
		$ret = $httpobj->request($url, $keysArr);
		$trytime = 3;
		for($i = 0; $i < $trytime; $i++)
		{
			if ($httpobj->info['http_code'] != 200)
			{
				$ret = $httpobj->request($url, $keysArr);
			}
			else
			{
				break;
			}
		}
		return $ret;
	}
	public function http_post_json($url, $data_string, $flag = 0) 
	{
		$httpobj = new Http;
		//$httpobj->content_type = "application/json";
		if (!$data_string)
		{
			$data_string = true;
		}
		$httpobj->headers = array('Content-Type: application/json');
		$return_content = $httpobj->request($url, $data_string);
		$trytime = 3;
		for($i = 0; $i < $trytime; $i++)
		{
			$return_code = $httpobj->info['http_code'];
			if ($return_code != 200)
			{
				//$httpobj->headers = "";
				$return_content = $httpobj->request($url, $data_string);
			}
			else
			{
				break;
			}
		}
		
		return array (
				$return_code,
				$return_content 
		);
	}
}
